//
//  DestinationDetailTableViewController.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/3/26.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "DestinationDetailTableViewController.h"
#import "StrDetailTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "CDHttpHelper.h"
#import "DesModel.h"
@interface DestinationDetailTableViewController ()
@property(nonatomic,strong)NSMutableArray * allDataArray;
@property(nonatomic,strong)NSMutableArray * sectionArray;
@property(nonatomic,strong)NSMutableArray * noteArray;
@property(nonatomic,strong)NSMutableArray * rowArray;
@property(nonatomic,assign)CGFloat widthForRow;

@end
#define url @"https://chanyouji.com/api/attractions/27394.json?page=1"
@implementation DestinationDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
     
     景点详情界面
     
     */
    [self.tableView registerClass:[StrDetailTableViewCell class] forCellReuseIdentifier:@"destinationDetail"];
    
    self.allDataArray = [NSMutableArray array];
    self.sectionArray = [NSMutableArray array];
    [self handleData];

    [self.tableView reloadData];
   
}

- (void)handleData{
    [[CDHttpHelper defaultCDHttpHelper]get:[NSString stringWithFormat:@"https://chanyouji.com/api/attractions/%ld.json?page=1",self.ID] params:nil success:^(id responseObj) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        if (dic != nil) {
            DesModel *model = [DesModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.allDataArray addObject:model];
            [self.sectionArray addObjectsFromArray:dic[@"attraction_trip_tags"]];
        }
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.sectionArray.count > 0) {
        return self.sectionArray.count;
    }else{
        return 1;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.sectionArray.count > 0) {
        NSArray *array = self.sectionArray[section][@"attraction_contents"];
        return array.count;
    }else{
        return 1;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *string;
    if (self.sectionArray.count > 0) {
       string = self.sectionArray[section][@"name"];
    }else{
        string = @"";
    }
   
    return string;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StrDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"destinationDetail" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[StrDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"destinationDetail"];
    }
    
    [self tableViewheaderView];
    
    self.widthForRow = 0;
    
    
    
    if (self.sectionArray.count > 0) {
        NSArray *array = self.sectionArray[indexPath.section][@"attraction_contents"];
        if (indexPath.row < array.count) {
            cell.titlelabel.text = array[indexPath.row][@"description"];
            cell.titlelabel.numberOfLines = 0;
            cell.titlelabel.frame = CGRectMake(20, 20, [UIScreen mainScreen].bounds.size.width - 40, [self getTrueHeightWithDescription:array[indexPath.row][@"description"]]);
            self.widthForRow = [self getTrueHeightWithDescription:array[indexPath.row][@"description"]];
            cell.descriptionlabel.frame = CGRectMake(10, CGRectGetMaxY(cell.titlelabel.frame), 0, 0);
            NSArray *photoArray = array[indexPath.row][@"notes"];
            if (photoArray.count != 0) {
                CGFloat contentWidth = 0;
                for (int i = 0; i < photoArray.count; i++) {
                    CGFloat width = [photoArray[i][@"width"] floatValue] *150 / [photoArray[i][@"height"] floatValue];
                    if (width < 50) {
                        width = 50;
                    }
                    contentWidth += width;
                    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(contentWidth - width, 0, width, 150)];
                    imageView.layer.cornerRadius = 5;
                    imageView.layer.borderWidth = 1;
                    imageView.layer.masksToBounds = YES;
                    imageView.layer.borderColor = [UIColor whiteColor].CGColor;
                    [imageView sd_setImageWithURL:[NSURL URLWithString:photoArray[i][@"photo_url"]]];
                    [cell.imageScrollView addSubview:imageView];
                    
                    if (contentWidth <= [UIScreen mainScreen].bounds.size.width) {
                        cell.imageScrollView.frame = CGRectMake(5, CGRectGetMaxY(cell.descriptionlabel.frame) + 20, contentWidth, 150);
                    }else{
                        cell.imageScrollView.frame = CGRectMake(5, CGRectGetMaxY(cell.descriptionlabel.frame) + 20, [UIScreen mainScreen].bounds.size.width - 10, 150);
                    }
                    
                    cell.imageScrollView.contentSize = CGSizeMake(contentWidth, 150);
                }
                
                if (contentWidth > [UIScreen mainScreen].bounds.size.width) {
                    cell.imageScrollView.pagingEnabled = YES;
                }
            }else{
                cell.imageScrollView.frame = CGRectMake(5, CGRectGetMaxY(cell.descriptionlabel.frame), 0, 0);
               
            }
            
        }
        self.widthForRow = [self getTrueHeightWithDescription:array[indexPath.row][@"description"]];
    }else{
        cell.titlelabel.text = @"暂时什么都没有";
        self.widthForRow = 0;
    }
    
    return cell;
}

- (void)tableViewheaderView
{
    DesModel *model  = [DesModel new];
    if (self.allDataArray.count != 0) {
         model = self.allDataArray[0];
    }
    // 图片
    UIImageView *headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height / 3)];
    [headImage sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.tableView.frame.size.width / 6,self.tableView.frame.size.height * 2 / 9, self.tableView.frame.size.width * 3 / 4, self.tableView.frame.size.height / 28)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = model.name_zh_cn;
    titleLabel.textColor = [UIColor whiteColor];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    [headImage addSubview:titleLabel];
    
    // 副标题
    UILabel *subheadLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.tableView.frame.size.width / 6,self.tableView.frame.size.height * 4 / 15, self.tableView.frame.size.width * 3 / 4, self.tableView.frame.size.height / 28)];
    subheadLabel.backgroundColor = [UIColor clearColor];
    
    subheadLabel.text = model.name_en;
    subheadLabel.textColor = [UIColor whiteColor];
    [headImage addSubview:subheadLabel];
    
    // 头像
    UIImageView *headPortraitView = [[UIImageView alloc]initWithFrame:CGRectMake(self.tableView.frame.size.width / 25, CGRectGetMinY(titleLabel.frame), self.tableView.frame.size.width / 9, CGRectGetHeight(titleLabel.frame) + CGRectGetHeight(subheadLabel.frame))];
    headPortraitView.image = [UIImage imageNamed:@"traffic.png"];
    
    // 头像白底
    UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(headPortraitView.frame) - 2, CGRectGetMinY(headPortraitView.frame) - 2, CGRectGetWidth(headPortraitView.frame) + 4, CGRectGetHeight(headPortraitView.frame) + 4)];
    whiteView.backgroundColor = [UIColor whiteColor];
    
    [headImage addSubview:whiteView];
    [headImage addSubview:headPortraitView];
    
    // 切圆角
    whiteView.layer.cornerRadius = 5;
    whiteView.layer.masksToBounds = YES;
    headPortraitView.layer.cornerRadius = 5;
    headPortraitView.layer.masksToBounds = YES;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(headImage.frame) + [self getTrueHeightWithDescription:model.descriptions] + 200)];
    [view addSubview:headImage];
    
    UILabel *descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(headImage.frame), [UIScreen mainScreen].bounds.size.width - 20, [self getTrueHeightWithDescription:model.descriptions])];
    descriptionLabel.text = model.descriptions;
    descriptionLabel.numberOfLines = 0;
    [view addSubview:descriptionLabel];
    
    UIWebView *tipWebView = [[UIWebView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(descriptionLabel.frame), [UIScreen mainScreen].bounds.size.width - 20, 200)];
    NSString *string = [NSString stringWithFormat:@"<html> \n"
                           "<head> \n"
                           "<style type=\"text/css\"> \n"
                           "body {margin:0;font-size: %f;}\n"
                           "</style> \n"
                           "</head> \n"
                           "<body>%@</body> \n"
                           "</html>",17.0,model.tips_html];
    [tipWebView loadHTMLString:string baseURL:nil];
    [view addSubview:tipWebView];
    
    self.tableView.tableHeaderView = view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.widthForRow + 200;
}


- (CGFloat)getTrueHeightWithDescription:(NSString *)discuss
{
    CGRect rect = [discuss boundingRectWithSize:(CGSizeMake([UIScreen mainScreen].bounds.size.width, 200000)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil];
    return rect.size.height;
}

@end
